//
//  VVHomeTableViewCell.swift
//  Weibo
//
//  Created by 王惠平 on 2017/3/31.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit
import SnapKit

class VVHomeTableViewCell: UITableViewCell {
    
    //原创微博视图
    fileprivate lazy var originalView:VVHomeOriginalView = VVHomeOriginalView()
    
    //toolBar
    fileprivate lazy var toolBar:VVHomeToolBarView = VVHomeToolBarView()
    
    //转发微博视图
    fileprivate lazy var retweetView:VVHomeRetWeetView = VVHomeRetWeetView()
    
    //记录toolBar的顶部约束
    var toolBarTopConstraint: Constraint?
    
    
    //绑定数据
    var statusViewModel: VVStatusViewModel? {
        
        didSet {
            
            originalView.statusViewModel = statusViewModel
            toolBar.statusViewModel = statusViewModel
            retweetView.statusViewModel = statusViewModel
        
        //把之前的toolBar的顶部约束卸载掉,重写根据数据创建对应的顶部约束,要保证toolbar上面只有一条顶部约束
            toolBarTopConstraint?.deactivate()
        
        //判断是否有赚微博
            if statusViewModel?.status?.retweeted_status != nil {
                
                //
                toolBar.snp.makeConstraints({ (make) in
                    
                    toolBarTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
                })
                
                //有转发微博
                retweetView.statusViewModel = statusViewModel
                
                //有转发显示转发视图
                retweetView.isHidden = false
            } else {
                
                //没有转发微博
                //隐藏转发微博视图
                retweetView.isHidden = true
                
                toolBar.snp.makeConstraints({ (make) in
                    
                    toolBarTopConstraint = make.top.equalTo(originalView.snp.bottom).constraint
                })
            }
        
        }
        
    }
    
    //首先创建cell代码
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加约束
    fileprivate func setupUI() {
        
        //设置contentView的背景色
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
        
        //添加子控件
        contentView.addSubview(originalView)
        contentView.addSubview(toolBar)
        contentView.addSubview(retweetView)
        
        originalView.snp.makeConstraints { (make) in
            
            make.top.equalTo(contentView).offset(8)
            make.leading.trailing.equalTo(contentView)
            //make.height.equalTo(100)
        }
        
        retweetView.snp.makeConstraints { (make) in
            
            make.top.equalTo(originalView.snp.bottom)
            make.leading.trailing.equalTo(originalView)
        }
        
        toolBar.snp.makeConstraints { (make) in
            
            //记录toolbar的顶部约束
           toolBarTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
            make.left.right.equalTo(retweetView)
            make.height.equalTo(35)
            
            //确定cell的高度
            make.bottom.equalTo(contentView)
        }
       
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
